let express = require('express')
let path = require('path')
let request = require('request')
let app = express()
app.use(express.static('dist'))

// send all requests to index.html so browserHistory in React Router works
let CDZHEARTBEAT_HOST = process.env.CDZHEARTBEAT_HOST || 'http://10.200.0.2:1095'

app.all('*', (req, res) => {
  if (/^\/api/.test(req.url)) {
    let url = CDZHEARTBEAT_HOST + req.url
    req.pipe(request(url)).pipe(res)
  } else {
    res.sendFile(path.join(__dirname, 'dist/index.html'))
  }
})

let PORT = process.env.PORT || 8080
app.listen(PORT, () => {
  console.log(`Production Express server running at :${PORT}. And fetch data from:${CDZHEARTBEAT_HOST}`)
})
